package ar.edu.itba.cg.score;

import com.jme.system.DisplaySystem;

public class Player implements Comparable<Player>{

	private int shootCounter;
	private BoardScore boardScore;
	private PointBoardScore currentPointBoard;
	private PointBoardScore firstPointBoard;
	private PointBoardScore secondPointBoard;
	private int total;
	
	public Player(int id, DisplaySystem display){
		boardScore= new BoardScore(display, id);
		shootCounter=0;
		total=0;
	}
	
	
	public void setFirstShoot(int point){
		shootCounter++;
		firstPointBoard= secondPointBoard;
		secondPointBoard= currentPointBoard;
		currentPointBoard= boardScore.addNewPoint(shootCounter, point);
		
		if(isStrike(firstPointBoard) && isStrike(secondPointBoard)){
			total+=20+point;
			firstPointBoard.setTotal(total);
		}else if(isSpare(secondPointBoard)){
			total+=10+point;
			secondPointBoard.setTotal(total);
			if(shootCounter==11){
				currentPointBoard.setTotal(total);
			}
		}
		
		
	}
	
	public void setSecondShoot(int point){
		currentPointBoard.setSecond(point);
		
		if(isStrike(secondPointBoard)){
			total+=10+ currentPointBoard.getFirst()+ point;
			secondPointBoard.setTotal(total);
		}
		
		if(shootCounter==11){
			currentPointBoard.setTotal(total);
		}else if(!isSpare(currentPointBoard)){
			total+=currentPointBoard.getFirst()+currentPointBoard.getSecond();
			currentPointBoard.setTotal(total);
		}
	}
	
	
	
	public boolean isStrike(PointBoardScore p){
		if(p==null)
			return false;
		
		return p.getFirst()==10;
	}
	
	public boolean isSpare(PointBoardScore p){
		if(p==null)
			return false;
		
		return p.getFirst()!=10&&(p.getFirst()+p.getSecond())==10;
	}


	public BoardScore getBoardScore() {
		return boardScore;
	}


	public int getShootCounter() {
		return shootCounter;
	}


	public boolean continueShooting() {
		if(shootCounter<11 || 
				(shootCounter==11 && isStrike(secondPointBoard) && 
						currentPointBoard.getSecond()==-1)){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public int compareTo(Player o) {
		return o.total-total;
	}
	
	
	
}
